import { createApp, h } from 'vue'
import Toast from './index.vue'

let toastDiv: HTMLDivElement

const showToast = props => {
  if (!toastDiv) {
    toastDiv = document.createElement('div')
  }
  const element = createApp({
    render() {
      return h(Toast, props)
    },
  })
  document.body.appendChild(toastDiv)
  element.mount(toastDiv)
  // 卸载
  setTimeout(() => {
    element.unmount()
    document.body.removeChild(toastDiv)
  }, props.duration || 2000)
}

export default showToast
